﻿/*----------------------------------------------------------------
//  <copyright file="ServiceExtensions.cs" company="MicroCloud@151504200868">
//      Copyright © 2020-2024 MicroCloud Corporation, All rights reserved.
//  </copyright>
//  <site>https://gitee.com/chenmm123/microclouds</site>
//  <last-editor>cmm</last-editor>
//  <last-date>2023-11-02 08:59</last-date>
//----------------------------------------------------------------*/

namespace MicroCloud.AspNetCore
{
    #region "<see cref="IServiceCollection"/> 扩展方法"
    /// <summary>
    /// <see cref="IServiceCollection"/> 扩展方法
    /// </summary>
    public static class ServiceExtensions
    {
        #region "扩展方法 - IServiceCollection"
        #region "获取 <see cref="IWebHostEnvironment"/> 环境信息"
        /// <summary>
        /// 获取 <see cref="IWebHostEnvironment"/> 环境信息
        /// </summary>
        /// <param name="services">服务集合</param>
        /// <returns></returns>
        public static IWebHostEnvironment GetWebHostEnvironment(this IServiceCollection services)
        {
            return services.GetSingleton<IWebHostEnvironment>();
        }
        #endregion

        #endregion

        #region "扩展方法 - IServiceProvider"
        #region "获取 <see cref="HttpContext"/> 实例"
        /// <summary>
        /// 获取 <see cref="HttpContext"/> 实例
        /// </summary>
        /// <param name="provider">服务提供者</param>
        /// <returns></returns>
        public static HttpContext GetHttpContext(this IServiceProvider provider)
        {
            IHttpContextAccessor accessor = provider.GetService<IHttpContextAccessor>();
            return accessor?.HttpContext;
        }
        #endregion
        #region "当前业务是否处于 HttpRequest 中"
        /// <summary>
        /// 当前业务是否处于 HttpRequest 中
        /// </summary>
        /// <param name="provider">服务提供者</param>
        /// <returns></returns>
        public static bool InHttpRequest(this IServiceProvider provider)
        {
            var context = provider.GetHttpContext();
            return context != null;
        }
        #endregion

        #endregion
    }
    #endregion

}
